home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / GCC / CC1 / !gcc / files / c / ackermann next >
Text File  |  1996-10-27  |  839b  |  51 lines

  1. /* Ackermann function */
  2.  
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6.  
  7. int ack (int, int, int);
  8.  
  9. int
  10. main (int argc, char *argv[])
  11. {
  12.   int a, b;
  13.  
  14.   if (argc == 1 || argc == 2)
  15.     {
  16.       printf ("Error. Too few parameters passed on command line\n");
  17.       printf ("Syntax: ackermann <x> <y>\n");
  18.       return 1;
  19.     }
  20.  
  21.   /* Convert from strings to numbers, the two arguments passed on the
  22.      command line.  */
  23.   a = atoi (argv[1]);
  24.   b = atoi (argv[2]);
  25.  
  26.   printf ("ack(%d,%d) = %d\n", a, b, ack (a, b, 0));
  27.   printf ("Time = %d\n", clock ());
  28.   return 0;
  29. }
  30.  
  31. int
  32. ack (register int x, register int y, register int acker)
  33. {
  34.   if (x == 0)
  35.     {
  36.       acker = y + 1;
  37.     }
  38.   else
  39.     {
  40.       if (y == 0)
  41.     {
  42.       acker = ack (x - 1, 1, 0);
  43.     }
  44.       else
  45.     {
  46.       acker = ack (x - 1, ack (x, y - 1, 0), 0);
  47.     }
  48.     }
  49.   return acker;
  50. }
  51.